package com.gexiaobao.pigeon.ui.fragment.mall

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import com.flyjingfish.openimagelib.OpenImage
import com.flyjingfish.openimagelib.enums.MediaType
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initFooter
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.ext.showLoading
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.FragmentMallProductBinding
import com.gexiaobao.pigeon.ui.adapter.MallShopAdapter
import com.gexiaobao.pigeon.ui.adapter.OnItemClickListener
import com.gexiaobao.pigeon.ui.mall.MallViewModel
import com.kingja.loadsir.core.LoadService
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction

/**
 * @Author      : hxw
 * @Date        : 2024/3/26 9:04
 * @Describe    : 店铺列表
 */
class FragmentMallShop : BaseFragment<MallViewModel, FragmentMallProductBinding>() {

    private var id = ""
    private lateinit var loadsir: LoadService<Any>
    private val mAdapter: MallShopAdapter by lazy { MallShopAdapter(arrayListOf()) }
    override fun initView(savedInstanceState: Bundle?) {
        id = requireArguments().getString("id").toString()

        mDatabind.layoutList.swipeRecyclerView.init(LinearLayoutManager(context), mAdapter).let {
            it.addItemDecoration(SpaceItemDecoration(0, Util.dp2px(context, 10f)))
            it.initFooter {
                //触发加载更多时请求数据
                mViewModel.getAppShopList(false, id.toInt())
            }
        }

        //状态页配置
        loadsir = loadServiceInit(mDatabind.layoutList.swipeRefreshLayout) {
            //点击重试时触发的操作
            refreshData(true)
        }

        mDatabind.layoutList.swipeRefreshLayout.init {
            refreshData(true)
        }

        mAdapter.setOnItemShopClickListener(object : MallShopAdapter.OnItemShopClickListener {
            override fun onItemClick(position: Int) {
                nav().navigateAction(R.id.action_to_fragment_merchant_store, Bundle().apply {
                    putInt("shopId", mAdapter.data[position].shopId)
                })
            }
        })

        mAdapter.setOnItemShopGoodsClickListener(object : MallShopAdapter.OnItemShopGoodsClickListener {
            override fun onItemClickShopGoodsListener(positionShop: Int, positionGoods: Int) {
                nav().navigateAction(R.id.action_to_fragment_goods_detail, Bundle().apply {
                    putString("id", mAdapter.data[positionShop].goodList[positionGoods].goodId.toString())
                })
            }
        })

        mAdapter.addChildClickViewIds(R.id.btnGoInShop, R.id.ivItemShopHead)
        mAdapter.setOnItemChildClickListener { _, view, position ->
            when (view.id) {
                R.id.btnGoInShop -> {
                    nav().navigateAction(R.id.action_to_fragment_merchant_store, Bundle().apply {
                        putInt("shopId", mAdapter.data[position].shopId)
                    })
                }

                R.id.ivItemShopHead -> {
                    showBigImage(view as ImageView, mAdapter.data[position].shopPicUrl)
                }
            }
        }
    }

    private fun showBigImage(view: ImageView, imageUrl: String) {
        OpenImage.with(context)
            .setClickImageView(view)
            .setSrcImageViewScaleType(ImageView.ScaleType.CENTER_CROP, true)
            .setImageUrl(imageUrl, MediaType.IMAGE).show()
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        refreshData(true)
    }

    private fun refreshData(isRefresh: Boolean) {
        loadsir.showLoading()
        mViewModel.getAppShopList(isRefresh, id.toInt())
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.shopListResult.observe(viewLifecycleOwner) {
            loadListData(it, mAdapter, loadsir, mDatabind.layoutList.swipeRecyclerView, mDatabind.layoutList.swipeRefreshLayout)
        }
    }

    companion object {
        fun newInstance(id: String): FragmentMallShop {
            val fragment = FragmentMallShop()
            val bundle = Bundle()
            fragment.arguments = bundle
            bundle.putString("id", id)
            return fragment
        }
    }
}